package git.soulbgm.controller;

import git.soulbgm.config.SysConfig;
import git.soulbgm.log.DebugInfo;
import git.soulbgm.log.LogOutput;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

/**
 * 调试控制器
 *
 * @author SoulBGM
 * @date 2024/03/01
 */
@RestController
public class DebugController {

    private final SysConfig config;

    public DebugController(SysConfig config) {
        this.config = config;
    }

    /**
     * 开启或关闭Debug模式
     *
     * @return {@link String}
     */
    @GetMapping(path = "debug")
    public String debug() {
        config.setDebug(!config.isDebug());
        return "已" + (config.isDebug() ? "开启" : "关闭") + "Debug";
    }

    /**
     * 列表日志输出
     *
     * @return {@link String}
     */
    @GetMapping("listLogOutput")
    public String listLogOutput() {
        Map<String, DebugInfo> debugInfoMap = LogOutput.getDebugInfoMap();
        StringBuilder sb = new StringBuilder();
        Set<String> keySet = debugInfoMap.keySet();
        for (String s : keySet) {
            DebugInfo debugInfo = debugInfoMap.get(s);
            sb.append(debugInfo.getName());
            sb.append(" > ");
            sb.append(debugInfo.getDescription());
            sb.append(" > ");
            sb.append(debugInfo.getDebug() ? "开启" : "关闭");
            sb.append("   \n");
        }
        return sb.toString();
    }

    /**
     * 开启或关闭某个日志的输出
     *
     * @param name 名称
     * @return {@link String}
     */
    @GetMapping("logOutput")
    public String logOutput(String name) {
        Boolean debug = LogOutput.setDebug(name);
        return "已" + (debug ? "开启" : "关闭") + name;
    }

}
